
package edu.weber.bookstore.database;

import edu.weber.bookstore.util.DatabaseConnection;
import edu.weber.bookstore.util.DatabaseModel;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 
 * 
 */
public class Professor extends DatabaseModel
{
	/**
	 * table tied to the database
	 */
	protected static String TABLE_NAME = "professors";
	private String _fname;
	private String _lname;
	
	/**
	 * Constructor for a Professor
	 * @param id the primary key for the Professor
	 * @param fname the first name of the professor
	 * @param lname the last name of the professor
	 */
	public Professor(int id, String fname, String lname)
	{
		super(id);
		_fname = fname;
		_lname = lname;
	}
	
	/**
	 * Returns the professor requested by the primary key
	 * @param id the primary key of the professor
	 * @return a professor whose primary key is the requested or null if not found.
	 * @throws SQLException 
	 */
	public static Professor find(int id) throws SQLException
	{
		Professor professor = null;
		
		PreparedStatement pm = DatabaseConnection.getConnection().prepareStatement("SELECT * FROM ".concat(TABLE_NAME).concat(" WHERE id = ?;"));
		pm.setInt(1, id);
		
		ResultSet rs = pm.executeQuery();
		
		int id_in = rs.findColumn("id");
		int name_in = rs.findColumn("fname");
		int description_in = rs.findColumn("lname");
		if(rs.next())
		{
			professor = new Professor(rs.getInt(id_in), rs.getString(name_in), rs.getString(description_in));
		}
		return professor;
	}
	
	@Override
	protected String getTableName()
	{
		return TABLE_NAME;
	}

	/**
	 * Unimplemented
	 * @throws SQLException 
	 */
	@Override
	protected void create() throws SQLException
	{
		throw new SQLException("No database table tied to this model.");
	}
	
	/**
	 * Unimplemented
	 * @throws SQLException 
	 */
	@Override
	protected void update() throws SQLException
	{
		throw new SQLException("No database table tied to this model.");
	}
	
	/**
	 * 
	 * @return first name of the professor
	 */
	public String getFname()
	{
		return _fname;
	}
	
	/**
	 * 
	 * @return last name or sir name of the professor
	 */
	public String getLname()
	{
		return _lname;
	}
	
	/**
	 * Sets the first name for the professor
	 * @param fname the requested first name
	 */
	public void setFname(String fname)
	{
		_fname = fname;
	}
	
	/**
	 * Sets the last name or sir name of the professor
	 * @param lname the requested last name or sir name
	 */
	public void setLname(String lname)
	{
		_lname = lname;
	}
}
